package com.ysd.lis.controller.sys;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysOrganization;
import com.ysd.lis.entity.SysRole;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.SysUserRole;
import com.ysd.lis.request.OrganizationPageParam;
import com.ysd.lis.service.SysOrganizationService;
import com.ysd.lis.service.SysRoleService;
import com.ysd.lis.service.SysUserRoleService;
import com.ysd.lis.service.SysUserService;
import com.ysd.lis.vo.OrgLabUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_organization 前端控制器
 * </p>
 *
 * @author Peng
 * @since 2021-10-08
 */
@Api(tags = "组织机构管理")
@RestController
@RequestMapping("/api/sys")
public class SysOrganizationController extends BaseController {

    @Autowired
    SysOrganizationService sysOrganizationService;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    SysUserRoleService sysUserRoleService;
    @Autowired
    SysUserService sysUserService;

    @ApiOperation("查询所有机构部门")
    @GetMapping("/findOrgAllList")
    public Result findOrgAllList() {
        return sysOrganizationService.findOrgAllList();
    }

    @ApiOperation("查询所有机构部门")
    @GetMapping("/findOrgTree")
    public Result findOrgTree() {
        return sysOrganizationService.findOrgTree();
    }

    @ApiOperation("根据组织机构ID查出机构下所有的机构及部门")
    @GetMapping("/findOrgDeptTreeByOrgId")
    public Result findOrgDeptTreeByOrgId(String orgId) {
        return sysOrganizationService.findOrgDeptTreeByOrgId(orgId);
    }


    @ApiOperation("根据类型查询机构部门")
    @RequestMapping(value = "/findOrganizationPageList", method = RequestMethod.GET)
    public Result findOrganizationPageList(OrganizationPageParam companyPage) {
        return sysOrganizationService.findOrganizationPageList(companyPage);
    }

    @ApiOperation("根据用户查询机构部门(无分页)")
    @RequestMapping(value = "/findOrganizationList", method = RequestMethod.GET)
    public Result findOrganizationList(OrganizationPageParam companyPage) {
        return sysOrganizationService.findOrganizationList(companyPage);
    }

    @ApiOperation("添加组织机构")
    @PostMapping("/addOrg")
    @LogDetail(requestParam ="addOrg",operationType = OperationType.INSERT, module = "组织机构管理",content = "添加组织机构操作")
    public Result addOrg(@RequestBody SysOrganization sysOrganization) {
        sysOrganization.setDelFlag(0);
        sysOrganization.setAppSecret(SecureUtil.md5("123qwe"));
        return sysOrganizationService.addOrg(sysOrganization);
    }

    @ApiOperation("修改组织机构")
    @PostMapping("/editOrg")
    @LogDetail(requestParam ="editOrg",operationType = OperationType.UPDATE, module = "组织机构管理",content = "修改组织机构操作")
    public Result editOrg(@RequestBody SysOrganization sysOrganization) {
        return sysOrganizationService.editOrg(sysOrganization);
    }

    @ApiOperation("删除组织机构")
    @PostMapping("/deleteOrg")
    @LogDetail(operationType = OperationType.DELETE, module = "删除组织机构")
    public Result deleteOrg(@RequestBody SysOrganization sysOrganization) {
        sysOrganization.setDelFlag(1);
        return sysOrganizationService.deleteOrg(sysOrganization);
    }

    @ApiOperation("删除组织机构")
    @DeleteMapping("/deleteOrgOrDept/{id}")
    @LogDetail(requestParam ="deleteOrgOrDept",operationType = OperationType.DELETE, module = "组织机构管理",content = "删除组织机构操作")
    public Result deleteOrgOrDept(@PathVariable("id") String id) {
        OrganizationPageParam companyPage = new OrganizationPageParam();
        companyPage.setId(id);
        companyPage.setOrgDeptType("1");
        return sysOrganizationService.deleteOrgOrDept(companyPage);
    }

    @ApiOperation("根据ID查询组织机构")
    @GetMapping("/findOrgById")
    public Result findOrgById(@RequestParam String id) {
        return sysOrganizationService.findOrgById(id);
    }

    @ApiOperation("根据ID查询子组织机构")
    @GetMapping("/findOrgChildList")
    public Result findOrgChildList(@RequestParam String orgId) {
        return sysOrganizationService.findOrgChildList(orgId);
    }

    @ApiOperation("查询组织机构父节点")
    @GetMapping("/findOrgNoParentList")
    public Result findOrgNoParentList(@RequestParam String orgId, Integer type) {
        return sysOrganizationService.findOrgNoParentList(orgId, type);
    }

    @ApiOperation("设置父机构")
    @PostMapping("setOrgParentIdList")
    public Result setOrgParentIdList(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println(parameterMap.size());
        List<Map> list = new ArrayList<>();
        for (int i = 0; i < parameterMap.size() / 2; i++) {
            Map map = new HashMap();
            String id = "";
            String parentId = "";
            if (parameterMap.get("orgList[" + i + "][id]") != null) {
                id = parameterMap.get("orgList[" + i + "][id]")[0];
            }
            if (parameterMap.get("orgList[" + i + "][parentId]") != null) {
                parentId = parameterMap.get("orgList[" + i + "][parentId]")[0];
            }
            map.put("id", id);
            map.put("parentId", parentId);
            list.add(map);
        }
        return sysOrganizationService.setOrgParentIdList(list);
    }

    @ApiOperation("获取所有角色")
    @GetMapping("/findAllRole")
    public Result findAllRole() {
        List<SysRole> list = sysRoleService.list();
        return Result.succ(1, "查询成功", list);
    }

    @ApiOperation("获取组织机构实验室初始人员信息")
    @GetMapping("/findLabUserByOrgId")
    public Result findLabUserByOrgId(String orgId) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getOrgId, orgId).eq(SysUser::getInitializer, "1");
        SysUser one = sysUserService.getOne(lambdaQueryWrapper);
        OrgLabUserVo orgLabUserVo = new OrgLabUserVo();
        if (one != null) {
            BeanUtils.copyProperties(one, orgLabUserVo);
            LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(SysUserRole::getUserId, one.getId());
            List<SysUserRole> list = sysUserRoleService.list(lambdaQueryWrapper1);
            if (CollectionUtils.isNotEmpty(list)) {
                List<String> collect = list.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
                orgLabUserVo.setRoles(collect);
            }
        }
        return Result.succ(1, "查询成功", orgLabUserVo);
    }

    @ApiOperation("新增实验室人员")
    @PostMapping("/addLabUser")
    public Result addLabUser(@RequestBody OrgLabUserVo orgLabUserVo) {
        return sysOrganizationService.addLabUser(orgLabUserVo);
    }
}
